<template>
  <div>
    <a v-if="item" slot="enterButton" href="javascript:;" @click="onSearchDepUser">事件分派</a>
    <a-button v-else slot="enterButton" :disabled="disabled" @click="onSearchDepUser">事件分派</a-button>
    <j-select-user-by-dep-modal
      ref="selectModal"
      :isDepFiltrate="isDepFiltrate"
      :modal-width="modalWidth"
      :multi="multi"
      @ok="selectOK"
      :user-ids="value"
      :store="storeField"
      :text="textField"
      @initComp="initComp"/>
  </div>
</template>

<script>
  import JSelectUserByDepModal from './JSelectUserByDepModal'
  import { underLinetoHump } from '@/components/_util/StringUtil'
  import { assignment } from '@/api/xlgc/allot'

  export default {
    name: 'JSelectUserByDep',
    components: { JSelectUserByDepModal },
    props: {
      eventId:String,
      eventType:String,
      item: {
        type: Boolean
      },
      modalWidth: {
        type: Number,
        default: 1250,
        required: false
      },
      value: {
        type: String,
        required: false
      },
      disabled: {
        type: Boolean,
        required: false,
        default: false
      },
      multi: {
        type: Boolean,
        default: true,
        required: false
      },
      backUser: {
        type: Boolean,
        default: false,
        required: false
      },
      // 存储字段 [key field]
      store: {
        type: String,
        default: 'username',
        required: false
      },
      // 显示字段 [label field]
      text: {
        type: String,
        default: 'realname',
        required: false
      },
      // 是否默认根据部门筛选
      isDepFiltrate: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data() {
      return {
        storeVals: '', // [key values]
        textVals: '' // [label values]
      }
    },
    computed: {
      storeField() {
        let field = this.customReturnField
        if (!field) {
          field = this.store
        }
        return underLinetoHump(field)
      },
      textField() {
        return underLinetoHump(this.text)
      }
    },
    mounted() {
      this.storeVals = this.value
    },
    watch: {
      value(val) {
        this.storeVals = val
      }
    },
    model: {
      prop: 'value',
      event: 'change'
    },
    methods: {
      initComp(textVals) {
        this.textVals = textVals
      },
      // 返回选中的用户信息
      backDeparInfo() {
        if (this.backUser === true) {
          if (this.storeVals && this.storeVals.length > 0) {
            let arr1 = this.storeVals.split(',')
            let arr2 = this.textVals.split(',')
            let info = []
            for (let i = 0; i < arr1.length; i++) {
              info.push({
                value: arr1[i],
                text: arr2[i]
              })
            }
            this.$emit('back', info)
          }
        }
      },
      onSearchDepUser() {
        this.$refs.selectModal.showModal()
      },
      selectOK(rows) {
        console.log('当前选中用户', rows)
        let data = {
          assigneeDepartId:rows[0].orgCode,
          assigneeId:rows[0].id,
          eventId:this.eventId,
          eventType:this.eventType,
          remarks:rows[1]
        }
        assignment(data).then(res => {
          console.log(res)
          if(res.success)
            this.$message.success('分派成功', 2)
          else
            this.$message.error(res.message,2)

        }).catch(err => {

        })
        // if (!rows) {
        //   this.storeVals = ''
        //   this.textVals = ''
        // } else {
        //   let temp1 = []
        //   let temp2 = []
        //   for (let item of rows) {
        //     temp1.push(item[this.storeField])
        //     temp2.push(item[this.textField])
        //   }
        //   this.storeVals = temp1.join(',')
        //   this.textVals = temp2.join(',')
        // }
        // this.$emit('change', this.storeVals)
        // this.$emit('rows', rows)
      }
    }
  }
</script>

<style scoped>

</style>
